package whu.cn.whushare.util;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.util.Collection;

import whu.cn.whushare.R;
import whu.cn.whushare.net.DownloadParams;

public class Utils {
	public static final int REFRESH_TO_COMPLETE = 1;

	public static String getPhotoUrl(String url) {
		if (!TextUtils.isEmpty(url) && !url.startsWith(DownloadParams.HR_URL)) {
			url = DownloadParams.HR_URL + url;
		}
		return url;
	}

	public static void setRefreshLabelToLast(Context context, PullToRefreshBase pullToRefreshBase) {
		pullToRefreshBase.getLoadingLayoutProxy(false, true).setPullLabel(context.getString(R.string.last_page));
		pullToRefreshBase.getLoadingLayoutProxy(false, true).setRefreshingLabel(context.getString(R.string.last_page));
		pullToRefreshBase.getLoadingLayoutProxy(false, true).setReleaseLabel(context.getString(R.string.last_page));
	}

	public static void resetRefreshLabel(Context context, PullToRefreshBase pullToRefreshBase) {
		pullToRefreshBase.getLoadingLayoutProxy(false, true).setPullLabel(
				context.getString(R.string.pull_to_refresh_pull_label));
		pullToRefreshBase.getLoadingLayoutProxy(false, true).setRefreshingLabel(
				context.getString(R.string.pull_to_refresh_release_label));
		pullToRefreshBase.getLoadingLayoutProxy(false, true).setReleaseLabel(
				context.getString(R.string.pull_to_refresh_refreshing_label));
	}

	public static void setRefreshLabel(Context context, PullToRefreshBase pullToRefreshBase, String str) {
		pullToRefreshBase.getLoadingLayoutProxy(false, true).setPullLabel(str);
	}

	public static Handler COMMON_HANDLER = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case REFRESH_TO_COMPLETE:
				PullToRefreshBase pullToRefreshBase = (PullToRefreshBase) msg.obj;
				pullToRefreshBase.onRefreshComplete();
				break;
			default:
				break;
			}
		}
	};

	public static boolean isCollectionEmpty(Collection c) {
		return c == null || c.size() == 0;
	}
}
